package com.pcbly.web.action.crm;

import java.util.Date;

import com.pcbly.web.action.BaseAction;
import com.pcbly.web.bean.crm.DbsqProductBean;
import com.pcbly.web.business.crm.DbsqProductService;
import com.pcbly.web.components.hibernate.support.Page;
import com.pcbly.web.components.form.AnalyseCommonDataExt;

/**
* @author carl.song
* Copyright (c) 2010 Lightsoft, International.
* All rights reserved.
*
*/

public class DbsqProductAction extends BaseAction<DbsqProductBean> {

	/**
	 * 
	 */
	private static final long serialVersionUID = -4111721672430287238L;

	protected DbsqProductService dbsqProductService; 
	
	public void setDbsqProductService(DbsqProductService dbsqProductService) {
		this.dbsqProductService = dbsqProductService;
	}
	private DbsqProductBean dbsqProduct;
	
	private String orderBy;
	
	private Page<DbsqProductBean> pages;
	
	private boolean asc = true;
	
	public boolean isAsc() {
		return asc;
	}

	public void setAsc(boolean asc) {
		this.asc = asc;
	}
	
	public String getOrderBy() {
		return orderBy;
	}

	public void setOrderBy(String orderBy) {
		this.orderBy = orderBy;
	}
	
	public DbsqProductBean getDbsqProduct() {
		return dbsqProduct;
	}
	public Page<DbsqProductBean> getPages() {
		return pages;
	}

	public void setPages(Page<DbsqProductBean> pages) {
		this.pages = pages;
	}
	public void setDbsqProduct(DbsqProductBean dbsqProduct) {
		this.dbsqProduct = dbsqProduct;
	}
	public String view() throws Exception {
		long startTime = System.currentTimeMillis();
		dbsqProduct=dbsqProductService.getDbsqProductBeanById(getId());
		extendCustomer = dbsqProductService.findDbsqProductBeanExtView(
				"t_crm_dbsqproduct_ext", getId());
				
		startTime = System.currentTimeMillis() - startTime;
		if (log.isDebugEnabled()) {
			log.debug("time ms:::::" + startTime);
			System.out.println("time ms:::::" + startTime);
		}
		return super.view();
	}
	public String update() throws Exception {
		long startTime = System.currentTimeMillis();
		setMyMessage(null);
		dbsqProduct=dbsqProductService.getDbsqProductBeanById(getId());
		extendCustomer = dbsqProductService.findDbsqProductBeanExtEdit(
				"t_crm_dbsqproduct_ext", getId());
		startTime = System.currentTimeMillis() - startTime;
		if (log.isDebugEnabled()) {
			log.debug("time ms:::::" + startTime);
			System.out.println("time ms:::::" + startTime);
		}
		return super.update();
	}
	public String index() throws Exception {
		long startTime = System.currentTimeMillis();

		long start = getPage();
		start = start * getPageSize();
		
		if (orderBy == null || "".equals(orderBy)) {
			orderBy = " id ";
		}
		pages = new Page<DbsqProductBean>();
		pages.setStart(start);
		pages.setPageSize(getPageSize());
		pages = dbsqProductService.findDbsqProductBeanByCond(
				dbsqProduct, pages, orderBy, asc);
		startTime = System.currentTimeMillis() - startTime;
		if (log.isDebugEnabled()) {
			log.debug("page:::::" + getPage());
			log.debug("pageSize:::::" + getPageSize());
			log.debug("time ms:::::" + startTime);

			System.out.println("page:::::" + getPage());
			System.out.println("pageSize:::::" + getPageSize());
			System.out.println("time ms:::::" + startTime);
		}
		return super.index();
	}
	public String add() throws Exception { 
		long startTime = System.currentTimeMillis();
		setMyMessage(null);
		dbsqProduct=new DbsqProductBean();
		startTime = System.currentTimeMillis() - startTime;
		if (log.isDebugEnabled()) {
			log.debug("time ms:::::" + startTime);
			System.out.println("time ms:::::" + startTime);
		}
		extendCustomer = dbsqProductService
				.findDbsqProductBeanExtCreate("t_crm_dbsqproduct_ext");
		return super.add();
	}
	public String save() throws Exception {
		long startTime = System.currentTimeMillis();
		if (log.isDebugEnabled()) {
			log.debug("login save..........");
			log.debug(dbsqProduct.getId());
		}
		AnalyseCommonDataExt ext = new AnalyseCommonDataExt();
		ext.setTableName("t_crm_dbsqproduct_ext");
		ext.setFeedbackKeyAndValue(feedbackKeyAndValue);
		
		try{
		if (dbsqProduct.getId() == null || "".equals(dbsqProduct.getId().trim())) {// new 
			dbsqProduct.setCreateDate(new Date());
			dbsqProduct.setCreateUserId(getUserId());
			dbsqProduct.setRecycled("0");
			dbsqProduct=dbsqProductService.saveDbsqProductBean(dbsqProduct);
			ext.setId(dbsqProduct.getId());
			String sql = ext.execute();
			if (!(sql == null || "".equals(sql.trim()))) {
				dbsqProductService.saveDbsqProductBeanExt(sql);
			}
			setMyMessage(getText("entity.saved"));
		} else {// update
			dbsqProductService.updateDbsqProductBean(dbsqProduct);
			
			boolean iflag = dbsqProductService.getHasDbsqProductBeanExtData(
					"t_crm_dbsqproduct_ext", dbsqProduct.getId());
			ext.setHaveData(iflag);
			ext.setId(dbsqProduct.getId());
			String sql = ext.execute();
			if (!(sql == null || "".equals(sql.trim()))) {
				dbsqProductService.saveDbsqProductBeanExt(sql);
			}
			
			setMyMessage(getText("entity.updated"));
		}}catch(Exception e){
			e.printStackTrace();
		}
		extendCustomer = dbsqProductService.findDbsqProductBeanExtEdit(
				"t_crm_dbsqproduct_ext", dbsqProduct.getId());
		startTime = System.currentTimeMillis() - startTime;
		if (log.isDebugEnabled()) {
			log.debug("time ms:::::" + startTime);
			System.out.println("time ms:::::" + startTime);
		}
		return super.save();
	}
	public String dwrSave(DbsqProductBean bean) throws Exception {
		long startTime = System.currentTimeMillis();
		if (log.isDebugEnabled()) {
			log.debug("login save..........");
			log.debug(bean.getId());
		}
		try{
		if (bean.getId() == null || "".equals(bean.getId())) {// new 
			dbsqProductService.saveDbsqProductBean(bean);
		} else {// update
			dbsqProductService.updateDbsqProductBean(bean);
		}}catch(Exception e){
			e.printStackTrace();
		}
		startTime = System.currentTimeMillis() - startTime;
		if (log.isDebugEnabled()) {
			log.debug("time ms:::::" + startTime);
			System.out.println("time ms:::::" + startTime);
		}
		return super.dwrBaseSave(bean.getId());
	}
	public String delete() throws Exception {
		long startTime = System.currentTimeMillis();
		if (log.isDebugEnabled()) {
			log.debug("login delete..........");
			log.debug(getId());
		}
		if(getId().indexOf(",")>=0){
			dbsqProductService.removeDbsqProductBeanByIds(getId());
		}else{
			dbsqProductService.removeDbsqProductBeanById(getId());
		}
		startTime = System.currentTimeMillis() - startTime;
		if (log.isDebugEnabled()) {
			log.debug("time ms:::::" + startTime);
			System.out.println("time ms:::::" + startTime);
		}
		dbsqProduct=null;
		return super.delete();
	}
	public String dwrDelete(String id) throws Exception {
		long startTime = System.currentTimeMillis();
		if (log.isDebugEnabled()) {
			log.debug("login delete..........");
			log.debug(id);
		}
		if(id.indexOf(",")>=0){
			dbsqProductService.removeDbsqProductBeanByIds(id);
		}else{
			dbsqProductService.removeDbsqProductBeanById(id);
		}
		startTime = System.currentTimeMillis() - startTime;
		if (log.isDebugEnabled()) {
			log.debug("time ms:::::" + startTime);
			System.out.println("time ms:::::" + startTime);
		}
		return super.dwrBaseDelete(id);
	}
	
}